<template>
  <el-drawer
    :title="drawerTitle[drawerType]"
    size="60%"
    :visible.sync="drawerVisible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="clearDialog"
    custom-class="tag-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <h4 class="type-title">
          <div class="icon-box"><icon-svg name="iconBase"></icon-svg></div>
          <span>基本信息</span>
        </h4>
        <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
          <el-row>
            <el-col :span="24">
              <el-form-item label="应用平台名称" prop="appPlfmNm">
                <el-input placeholder="请输入应用平台名称" v-model="editForm.appPlfmNm"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="应用平台描述" prop="appPlfmDesc">
                <el-input
                  type="textarea"
                  placeholder="请输入应用平台描述"
                  v-model="editForm.appPlfmDesc"
                  :rows="5"
                  resize="none"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="clearDialog">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  data() {
    return {
      drawerVisible: false,
      drawerType: 'add',
      drawerTitle: {
        add: '新增应用平台',
        edit: '修改应用平台'
      },
      editForm: {
        appPlfmId: '',
        appPlfmNm: '',
        appPlfmDesc: ''
      },
      oldMenuIds: [],
      editFormRules: {
        appPlfmNm: [
          { required: true, message: '应用平台名称不能为空', trigger: 'blur' },
          { max: 20, message: '应用平台名称不能超过20个字符', trigger: 'blur' }
        ],
        appSceneDesc: [{ max: 500, message: '应用平台描述不能超过500个字符', trigger: 'blur' }]
      }
    }
  },
  methods: {
    showDrawer(type, data) {
      this.drawerType = type
      if (this.drawerType === 'edit') {
        this.editForm.appPlfmId = data.appPlfmId
        this.editForm.appPlfmNm = data.appPlfmNm
        this.editForm.appPlfmDesc = data.appPlfmDesc
      }
      this.drawerVisible = true
    },
    submit() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          let params = {
            appPlfmNm: this.editForm.appPlfmNm,
            appPlfmDesc: this.editForm.appPlfmDesc
          }
          if (this.drawerType === 'edit') params.appPlfmId = this.editForm.appPlfmId
          this.$ajax({
            url: this.$ajax.setUrl(this.drawerType === 'add' ? 'ast/astAppPlatform/save' : 'ast/astAppPlatform/update'),
            method: 'post',
            data: this.$ajax.setData(params)
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.clearDialog()
              this.$emit('refresh')
              this.$message.success(data.msg)
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    clearDialog() {
      this.editForm.appPlfmId = ''
      this.editForm.appPlfmNm = ''
      this.editForm.appPlfmDesc = ''
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
        this.drawerVisible = false
      })
    }
  }
}
</script>

<style lang="scss"></style>
